//
//  SNHViewController.m
//  SNHStaticTableViewKit
//
//  Created by chenjianlei on 06/09/2018.
//  Copyright (c) 2018 chenjianlei. All rights reserved.
//

#import "SNHViewController.h"
#import <SNHStaticTableViewKit/SNHStaticTableViewKit.h>

@interface SNHViewController ()
@property (nonatomic, strong) SNHStaticTableViewInfo *tableViewInfo;
@end

@implementation SNHViewController


- (void)injected {
    NSLog(@"%@",self);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self.view addSubview:self.tableViewInfo.tableView];
    [self _addSectionOne];
    [self _addSectionTwo];
    [self _addsectionThree];
    [self _addSectionFour];
    [self.tableViewInfo.tableView reloadData];
}

- (void)_addSectionOne {
    SNHStaticTableViewCellInfo *age = [SNHStaticTableViewCellInfo normalCellForTitle:@"年龄" rightValue:@"18"];
    SNHStaticTableViewCellInfo *name = [SNHStaticTableViewCellInfo normalCellForTitle:@"名字" rightValue:@"陈建蕾"];
    SNHStaticTableViewSectionInfo *sectionInfo = [SNHStaticTableViewSectionInfo headerSectionInfoWithTitle:@"个人信息"];
    [sectionInfo addCellInfo:age];
    [sectionInfo addCellInfo:name];
    [self.tableViewInfo addSection:sectionInfo];
}

- (void)_addSectionTwo {
    SNHStaticTableViewCellInfo *switchCell = [SNHStaticTableViewCellInfo switchCellForTarget:self action:@selector(onActionChange:indexPath:) title:@"切换view颜色" isOn:YES];
    SNHStaticTableViewCellInfo *nextCell = [SNHStaticTableViewCellInfo normalCellForTarget:self action:@selector(onActionNext:indexPath:) title:@"下一页" rightValue:@"ok" accessoryType:UITableViewCellAccessoryDisclosureIndicator];
    SNHStaticTableViewCellInfo *center = [SNHStaticTableViewCellInfo centerCellForTarget:self action:@selector(onActionCenter:indexPath:) title:@"我是居中"];
    SNHStaticTableViewCellInfo *image = [SNHStaticTableViewCellInfo normalCellForTitle:@"图片" rightValue:@"yes" imageName:@"home_search"];
    SNHStaticTableViewSectionInfo *sectionInfo = [SNHStaticTableViewSectionInfo headerSectionInfoWithTitle:@"Action"];
    [sectionInfo addCellInfo:switchCell];
    [sectionInfo addCellInfo:nextCell];
    [sectionInfo addCellInfo:center];
    [sectionInfo addCellInfo:image];
    [self.tableViewInfo addSection:sectionInfo];
}

- (void)_addsectionThree {
    SNHStaticTableViewCellInfo *one = [SNHStaticTableViewCellInfo makeCellForTarget:self action:@selector(onActionThreeOne:indexPath:) makeTarget:self makeAction:@selector(makeTargetOnCell:sender:) height:88.f accessoryType:UITableViewCellAccessoryNone userInfo:@{@"name": @"chen", @"age": @19}];
    SNHStaticTableViewSectionInfo *section = [SNHStaticTableViewSectionInfo headerSectionInfoWithTitle:@"three"];
    [section addCellInfo:one];
    [self.tableViewInfo addSection:section];
}

- (void)_addSectionFour {
    SNHStaticTableViewCellInfo *one = [SNHStaticTableViewCellInfo normalCellForTitle:@"第四" rightValue:@"Four"];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 80)];
    headerView.backgroundColor = [UIColor redColor];
    SNHStaticTableViewSectionInfo *section = [SNHStaticTableViewSectionInfo headerSectionWithView:headerView];
    [section addCellInfo:one];
    [self.tableViewInfo addSection:section];
}

#pragma mark - Action
- (void)onActionChange:(SNHStaticTableViewCellInfo *)sender indexPath:(NSIndexPath *)indexPath {
    NSNumber *isOn = [sender getUserInfoForKey:@"on"];
    if (isOn.boolValue) {
        self.tableViewInfo.tableView.backgroundColor = [UIColor redColor];
    } else {
        self.tableViewInfo.tableView.backgroundColor = [UIColor orangeColor];
    }
}

- (void)onActionNext:(SNHStaticTableViewCellInfo *)sender indexPath:(NSIndexPath *)indexPath {
    UIViewController *vc = [UIViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onActionCenter:(SNHStaticTableViewCellInfo *)sender indexPath:(NSIndexPath *)indexPath {
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:nil];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
    [alertVc addAction:okAction];
    [alertVc addAction:cancelAction];
    [self presentViewController:alertVc animated:YES completion:nil];
}

- (void)makeTargetOnCell:(SNHStaticTableViewCell *)cell sender:(SNHStaticTableViewCellInfo *)sender {
    NSLog(@"%@", sender.userInfo);
}

- (void)onActionThreeOne:(SNHStaticTableViewCellInfo *)sender indexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - Private
- (SNHStaticTableViewInfo *)tableViewInfo {
    if (!_tableViewInfo) {
        _tableViewInfo = [[SNHStaticTableViewInfo alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    }
    return _tableViewInfo;
}

@end
